//
//  main.c
//  BatailleNavaleRoyale
//
//  Created by Amer Nasser on 14/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <sys/types.h>
#include "stringTools.h"
#include "joueur.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int mainzs(int argc ,char **argv){
    
    
	char nomJoueur[20];
	char connectCommand[30];
	char connectResponse[30];
    
    
	
    memset(nomJoueur,0,sizeof(nomJoueur));
    memset(connectCommand,0,sizeof(connectCommand));
    memset(connectResponse,0,sizeof(connectResponse));
	
    
    printf("Entrez le nom du joueur :\n");
	scanf("%s",nomJoueur);
    
    
    
    //mettre ce qu'on croit recevoir du serveur  dans connectResponse
    
    strcpy(connectResponse,"WELCOME/Nasser");
    
	printf("%s\n",connectResponse);
    
    char** mot=splitString(connectResponse, '/');
    int nbUser=countChars(connectResponse, '/');
    int i;
    
    //Enregistre le nom du joueur dans username
    if(strcmp(mot[0],"WELCOME")!=0){
        printf("BYE BYE\n");
        
        exit(3);
    }
    char *username="Nasser";
    if(nbUser ==2){
        username =mot[1];
    }
    else{
        username=malloc(sizeofString((connectResponse)-8) * sizeof(char));
        for(i=1;i<nbUser;i++){
            strcat(username,mot[i]);
            if((i+1)<nbUser){
                strcat(username,"/");
            }
        }
    }
    
    printf("Votre username sera : %s\n",username);
    
    joueur *player =createPlayer(username);
    
    
    //Les joueurs dans la partie  exemple /PLAYERS/amer/tissa/olivier/
    memset(nomJoueur,0,sizeof(nomJoueur));
    memset(connectCommand,0,sizeof(connectCommand));
    memset(connectResponse,0,sizeof(connectResponse));
    
    
    strcpy(connectResponse,"PLAYERS/amer/Nasser/");
    
	printf("%s\n",connectResponse);
    
    //Placement des sous-marin
    char confirmationResponse[15];
    
    int ship=0;
    int x=0,y=0,cmpX=0,cmpY=0;
    
    
    memset(connectCommand,0,sizeof(connectCommand));
    memset(confirmationResponse,0,sizeof(confirmationResponse));
    memset(connectResponse,0,sizeof(connectResponse));
    
    //premier SHIP/1/
    
    strcpy(connectResponse,"SHIP/1/");
    
    printf("%s\n",connectResponse);
    
    scanf("%s",connectCommand);
    
    //premier OK/
    strcpy(confirmationResponse,"OK/");
    
    
    printf("%s\n",confirmationResponse);
    
    if(strcmp(confirmationResponse, "OK/")==0){
        char** mot3=splitString(connectCommand, '/');
        int nbcoord=(countChars(connectCommand, '/')-1);
        for(i=1;i<=nbcoord;i++){
            if(i%2!=0){
                x=atoi(mot3[i]);
            }
            else{
                y=atoi(mot3[i]);
                player->subs[ship]->xy[cmpX]=createCoordonne(x,y);
                cmpX++;
            }
        }
        ship++; 
    }
    
    if(strcmp(confirmationResponse, "ALLYOURBASE/")==0){
        
    }
    
    
    memset(connectCommand,0,sizeof(connectCommand));
    memset(confirmationResponse,0,sizeof(confirmationResponse));
    memset(connectResponse,0,sizeof(connectResponse));
    
    //premier SHIP/1/
    
    strcpy(connectResponse,"SHIP/1/");
    
    printf("%s\n",connectResponse);
    
    
    //PUTSHIP/5/A/5/B/5/C/
    scanf("%s",connectCommand);
    
    //premier OK/
    strcpy(confirmationResponse,"OK/");
    
    
    printf("%s\n",confirmationResponse);
    
    if(strcmp(confirmationResponse, "OK/")==0){
        char** mot3=splitString(connectCommand, '/');
        int nbcoord=(countChars(connectCommand, '/')-1);
        for(i=1;i<=nbcoord;i++){
            if(i%2!=0){
                x=atoi(mot3[i]);
            }
            else{
                y=atoi(mot3[i]);
                player->subs[ship]->xy[cmpX]=createCoordonne(x,y);
                cmpX++;
            }
        }
        ship++; 
    }
    
    if(strcmp(confirmationResponse, "ALLYOURBASE/")==0){
        
    }
    
    
    memset(connectCommand,0,sizeof(connectCommand));
    memset(confirmationResponse,0,sizeof(confirmationResponse));
    memset(connectResponse,0,sizeof(connectResponse));
    
    //premier SHIP/1/
    
    strcpy(connectResponse,"SHIP/1/");
    
    printf("%s\n",connectResponse);
    
    
    //PUTSHIP/6/H/
    scanf("%s",connectCommand);
    
    //premier OK/
    strcpy(confirmationResponse,"OK/");
    
    
    printf("%s\n",confirmationResponse);
    
    if(strcmp(confirmationResponse, "OK/")==0){
        char** mot3=splitString(connectCommand, '/');
        int nbcoord=(countChars(connectCommand, '/')-1);
        for(i=1;i<=nbcoord;i++){
            if(i%2!=0){
                x=atoi(mot3[i]);
            }
            else{
                y=atoi(mot3[i]);
                player->subs[ship]->xy[cmpX]=createCoordonne(x,y);
                cmpX++;
            }
        }
        ship++; 
    }
    
    
    if(strcmp(confirmationResponse, "ALLYOURBASE/")==0){
        
    }
    
    
    
    
    
    
    memset(connectCommand,0,sizeof(connectCommand));
    memset(confirmationResponse,0,sizeof(confirmationResponse));
    memset(connectResponse,0,sizeof(connectResponse));
    
    
    //premier YOURTURN/8/H/
    
    strcpy(confirmationResponse,"YOURTURN/8/H/");
    
    
    printf("%s\n",connectResponse);
    
    char** mot4=splitString(confirmationResponse, '/');
    int nbcoord=(countChars(confirmationResponse, '/'));
    
    if(strcmp(mot4[0],"YOURTURN")==0){
        scanf("%s",connectCommand);
        
    }
    
    if(strcmp(mot4[0],"TOUCHE")==0){
        char ord=mot4[2][0];
        int ordonne='A'-ord;
        int absicce=atoi(mot4[1]);
        
        player->positions[absicce][ordonne]=2;
        
    }
    
    if(strcmp(mot4[0],"OUCH")==0){
        
        char ord=mot4[2][0];
        int ordonne='A'-ord;
        int absicce=atoi(mot4[1]);
        
        partOfSubDestroyed(player->subs, absicce,ordonne,7);
        
    }
    
    if(strcmp(mot4[0],"MISS")==0){
        char ord=mot4[2][0];
        int ordonne='A'-ord;
        int absicce=atoi(mot4[1]);
        
        player->positions[absicce][ordonne]=1;
        
    }
    
    if(strcmp(mot4[0],"AWINNERIS")==0){
        
        
    }
    
    if(strcmp(mot4[0],"DRAWGAME")==0){
        
        
    }
    
    
    memset(connectCommand,0,sizeof(connectCommand));
    memset(confirmationResponse,0,sizeof(confirmationResponse));
    memset(connectResponse,0,sizeof(connectResponse));
    
    
    strcpy(confirmationResponse,"OUCH/8/H/");
    
    
    printf("%s\n",connectResponse);
    
     mot4=splitString(confirmationResponse, '/');
    nbcoord=(countChars(confirmationResponse, '/'));
    
    if(strcmp(mot4[0],"YOURTURN")==0){
        scanf("%s",connectCommand);
        
    }
    
    if(strcmp(mot4[0],"TOUCHE")==0){
        char ord=mot4[2][0];
        int ordonne='A'-ord;
        int absicce=atoi(mot4[1]);
        
        player->positions[absicce][ordonne]=2;
        
    }
    
    if(strcmp(mot4[0],"OUCH")==0){
        
        char ord=mot4[2][0];
        int ordonne='A'-ord;
        int absicce=atoi(mot4[1]);
        
        partOfSubDestroyed(player->subs, absicce,ordonne,7);
        
    }
    
    if(strcmp(mot4[0],"MISS")==0){
        char ord=mot4[2][0];
        int ordonne='A'-ord;
        int absicce=atoi(mot4[1]);
        
        player->positions[absicce][ordonne]=1;
        
    }
    
    if(strcmp(mot4[0],"AWINNERIS")==0){
        
        
    }
    
    if(strcmp(mot4[0],"DRAWGAME")==0){
        
        
    }

    
    
    
    
    //FIN DU JEU
    
    //Demande d'un nouveau jeu ou de quitter
    
    memset(connectCommand,0,sizeof(connectCommand));
    memset(confirmationResponse,0,sizeof(confirmationResponse));
    memset(connectResponse,0,sizeof(connectResponse));
    
    return 0;
}
